import java.awt.EventQueue;

import javax.swing.JFrame;
import javax.swing.JScrollPane;
import java.awt.BorderLayout;
import javax.swing.JTable;
import javax.swing.event.TableModelListener;
import javax.swing.table.DefaultTableModel;
import javax.swing.table.TableModel;

import com.ha.sdk.HaCamera;

import javax.swing.JPanel;
import javax.swing.JList;
import javax.swing.AbstractListModel;
import javax.swing.JProgressBar;
import javax.swing.JButton;
import javax.swing.JLabel;
import javax.swing.JTextField;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;

public class Searchdevice {

	private JFrame frame;
	private JButton btnNewButton;
	private JLabel lblMac;
	private JTextField textField;
	private JLabel lblNewLabel;
	private JTextField textField_1;
	private JLabel lblNewLabel_1;
	private JTextField textField_2;
	private JLabel lblNewLabel_2;
	private JTextField textField_3;
	private JButton btnNewButton_1;

	/**
	 * Launch the application.
	 */
	public static void main(String[] args) {
		EventQueue.invokeLater(new Runnable() {
			public void run() {
				try {
					Searchdevice window = new Searchdevice();
					window.frame.setVisible(true);
				} catch (Exception e) {
					e.printStackTrace();
				}
			}
		});
	}

	/**
	 * Create the application.
	 */
	public Searchdevice() {
		initialize();
	}

	/**
	 * Initialize the contents of the frame.
	 */
	private void initialize() {
		frame = new JFrame();
		frame.setBounds(100, 100, 723, 551);
		frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		frame.getContentPane().setLayout(null);
		frame.setLocationRelativeTo(null);
		HaCamera.init();
		JTable table = new JTable();
		table.addMouseListener(new MouseAdapter() {
			@Override
			public void mouseClicked(MouseEvent e) {
				
				
				System.out.println(table.getValueAt(table.getSelectedRow(), 1)  );
				textField.setText(table.getValueAt(table.getSelectedRow(), 1).toString());
				textField_1.setText(table.getValueAt(table.getSelectedRow(), 0).toString());
				textField_2.setText(table.getValueAt(table.getSelectedRow(), 2).toString());
				String wg=table.getValueAt(table.getSelectedRow(), 0).toString();
				
				textField_3.setText(wg.substring(0, wg.lastIndexOf(".")+1)+"1");
				
			}
		});
		table.setModel(new DefaultTableModel(
			new Object[][] {
				
			},
			new String[] {
				"Ip", "Mac", "子网掩码", "厂商", "平台", "系统", "版本"
			}
		));
		
		
		HaCamera.onDeviceDiscovered((String mac, String ip, String netmask, String manufacturer, String platform,
				String system, String version)-> {
			// TODO Auto-generated method stub
			System.out.println(mac+","+ip+","+netmask+","+manufacturer+","+platform+","+system+","+version);
			
			DefaultTableModel mod=(DefaultTableModel) table.getModel();
			mod.addRow(new Object[] {ip, mac, netmask, manufacturer, platform, system, version});
			table.setModel(mod);
		
	    });
		
		
		
		
		
		
		JScrollPane js=new JScrollPane(table);
	
		js.setBounds(0, 208, 707, 304);
		frame.getContentPane().add(js);
		
		btnNewButton = new JButton("搜索设备");
		btnNewButton.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				int numT =table.getModel().getRowCount();//获取当前已有行数
				while (numT > 0) {//如果是全体刷新表格需要移除之前的所有数据行
				((DefaultTableModel)table.getModel()).removeRow(0);
				numT--;
				}
				
				
				HaCamera.discoverDevice();
				
			}
		});
		btnNewButton.setBounds(10, 37, 93, 23);
		frame.getContentPane().add(btnNewButton);
		
		lblMac = new JLabel("MAC:");
		lblMac.setBounds(10, 86, 54, 15);
		frame.getContentPane().add(lblMac);
		
		textField = new JTextField();
		textField.setBounds(37, 83, 128, 21);
		frame.getContentPane().add(textField);
		textField.setColumns(10);
		
		lblNewLabel = new JLabel("IP:");
		lblNewLabel.setBounds(176, 86, 54, 15);
		frame.getContentPane().add(lblNewLabel);
		
		textField_1 = new JTextField();
		textField_1.setBounds(200, 83, 110, 21);
		frame.getContentPane().add(textField_1);
		textField_1.setColumns(10);
		
		lblNewLabel_1 = new JLabel("掩码：");
		lblNewLabel_1.setBounds(342, 86, 54, 15);
		frame.getContentPane().add(lblNewLabel_1);
		
		textField_2 = new JTextField();
		textField_2.setBounds(379, 83, 110, 21);
		frame.getContentPane().add(textField_2);
		textField_2.setColumns(10);
		
		lblNewLabel_2 = new JLabel("网关：");
		lblNewLabel_2.setBounds(499, 86, 54, 15);
		frame.getContentPane().add(lblNewLabel_2);
		
		textField_3 = new JTextField();
		textField_3.setBounds(550, 83, 116, 21);
		frame.getContentPane().add(textField_3);
		textField_3.setColumns(10);
		
		btnNewButton_1 = new JButton("设置");
		btnNewButton_1.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				
				HaCamera.setNetInfoByMac(textField.getText() , textField_1.getText(), textField_2.getText(), textField_3.getText());
				
			}
		});
		btnNewButton_1.setBounds(573, 114, 93, 23);
		frame.getContentPane().add(btnNewButton_1);
	
		
		 
	
		
	}

}
